package com.gdou.im.server.codeC;

import com.alibaba.fastjson.JSONObject;
import com.gdou.im.protocol.Packet;
import com.gdou.im.protocol.PacketCodeC;
import com.gdou.im.protocol.command.Command;
import com.gdou.im.protocol.data.Data;
import com.gdou.im.protocol.data.request.MessageRequest;
import com.gdou.im.protocol.data.response.LoginResponse;
import com.gdou.im.protocol.data.response.MessageResponse;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageCodec;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.gdou.im.protocol.command.Command.LOGIN_RESPONSE;

/**
 * @ProjectName: demo
 * @Package: com.gdou.im.server.codeC
 * @ClassName: PacketToDateCodeC
 * @Author: carrymaniac
 * @Description:
 * @Date: 2020/6/16 5:32 下午
 * @Version:
 */
@Component
@Slf4j
public class PacketToDataCodeC extends MessageToMessageCodec<Packet, Data> {
    private final PacketCodeC packetCodeC;
    public PacketToDataCodeC(PacketCodeC packetCodeC) {
        this.packetCodeC = packetCodeC;
    }

    @Override
    protected void encode(ChannelHandlerContext ctx, Data data, List<Object> out) throws Exception {
        log.info("[PacketToDataCodeC-encode] data 内容为:{}",data);
        Packet packet = packetCodeC.encode(data);
        log.info("[PacketToDataCodeC-encode] packet 内容为:{}",packet);
        out.add(packet);
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, Packet packet, List<Object> out) throws Exception {
        //对Packet进行解析 拿出data进行处理
        log.info("[PacketToDataCodeC-decode] packet 内容为:{}",packet);
        Data decode = packetCodeC.decode(packet);
        log.info("[PacketToDataCodeC-decode] Data 内容为:{}",decode);
        out.add(decode);
    }
}
